<template>
	<view class="template-product tn-safe-area-inset-bottom">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar fixed customBack :bottomShadow="false" backgroundColor="#FFFFFF">
			<view slot="back" class='tn-custom-nav-bar__back' @click="goBack">
				<text class='icon tn-icon-left'></text>
				<text class='icon tn-icon-home-capsule-fill'></text>
			</view>
		</tn-nav-bar>
		<view class="tn-margin-top tn-margin-bottom" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-margin-bottom tn-margin-sm">
				<view class="justify-content-item">
					<view class="tn-flex tn-flex-col-center tn-flex-row-left">
						<view class="logo-pic tn-shadow">
							<view class="logo-image">
								<view class="tn-shadow-blur" style="width: 110rpx;height: 110rpx;">
									<image mode="aspectFill" style="width: 100%;height: 100%;" :src="avatar">
									</image>
								</view>
							</view>
						</view>
						<view class="tn-padding-right">
							<view class="tn-padding-right tn-padding-left-sm tn-text-xl tn-text-bold">
								<text class="tn-color-brown--dark">{{ nickname }}</text>
							</view>
							<view class="tn-padding-right tn-padding-top-xs tn-padding-left-sm tn-text-ellipsis">
								<text v-if="isagent==1 && status==1" class="tn-color-brown"
									style="opacity: 0.5;">{{levelName}}</text>
								<text class="tn-color-brown tn-text-bold tn-padding-left-sm">VIP {{ level }}</text>
							</view>
						</view>
						<view class="tn-padding-left-sm">
							<view class="tn-padding-left tn-padding-left-sm tn-text-xl tn-text-bold">
								<text class="tn-color-red">佣金总金额：</text>
							</view>
							<view class="tn-padding-right tn-padding-top-xs tn-padding-left-sm tn-text-ellipsis">
								<!-- <text class="tn-color-brown" style="opacity: 0.5;">认证会员</text> -->
								<text class="tn-color-red tn-text-bold tn-padding-left-sm">{{ sumMoney }}</text>
								<tn-button class="tn-padding-left-sm" backgroundColor="tn-bg-blue"
									fontColor="tn-color-white" width="100rpx"
									@click="withdrawRedpacket()">提现</tn-button>
							</view>

						</view>
					</view>
				</view>
			</view>
			<view class="tn-flex tn-flex-center tn-margin-sm" style="width: 100%;">
				<view class="tn-text-center"
					style="width: 95%; height: 90rpx;background-color: black;border-radius: 50rpx;">
					<text class="tn-flex tn-color-white tn-padding"
						style="height: 90rpx;line-height: 100%;">累计&nbsp;&nbsp;<text
							style="color:#ff1781">{{ invitee_num }}</text>&nbsp;&nbsp;人查看过您的推荐</text>
				</view>
			</view>
		</view>


		<view class="tn-padding-bottom-xl">

			<view class="tn-bg-white">
				<tn-sticky :offsetTop="0" :customNavHeight="vuex_custom_bar_height">
					<tn-tabs :list="fixedList" :current="current" :isScroll="false" activeColor="#000" bold="true"
						backgroundColor="#FFFFFF" :fontSize="32" :badgeOffset="[20, 50]" @change="tabChange"></tn-tabs>
				</tn-sticky>
			</view>
			<view class="" v-if="current==0">
				<view class="tn-margin" v-for="(item,index) in inviteeList" :key="index">
					<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding website-shadow">
						<view class="justify-content-item" style="width: 100%;">
							<view class="tn-flex tn-flex-col-center tn-flex-row-left">
								<view class="logo-pic">
									<view class="logo-image">
										<view class="tn-shadow-blur"
											:style="'background-image:url(' + item.avatar + ');width: 110rpx;height: 110rpx;background-size: cover;'">
										</view>
									</view>
								</view>
								<view class="tn-padding-right tn-color-black">
									<view class="tn-padding-right tn-padding-left-sm tn-text-ellipsis">
										{{ item.nickname }}
										<tn-tag v-if="item.level > 0" class="tn-margin-left-sm"
											backgroundColor="#ff557f" fontColor="#080808" shape="circle">已参加</tn-tag>
										<tn-tag v-else class="tn-margin-left-sm" backgroundColor="#01BEFF"
											fontColor="#080808" shape="circle">未参加</tn-tag>
									</view>
									<view
										class="tn-padding-right tn-padding-top-xs tn-text-ellipsis tn-padding-left-sm">
										<text class="tn-color-purplered">直推人数</text>
										<text
											class="tn-color-brown tn-padding-left-xs tn-text-bold">{{ item.invite_buy }}</text>
										<text class="tn-padding-left-sm tn-color-purplered">阅读人数</text>
										<text
											class="tn-color-brown tn-padding-left-xs tn-text-bold">{{ item.invite_nobuy }}</text>
									</view>
								</view>
							</view>
							<view class="tn-flex tn-flex-col-center tn-flex-row-right">
								<text class="tn-text-sm">{{ item.register_time }}</text>
							</view>
						</view>
					</view>
				</view>
				<tn-load-more v-if="inviteeList.length" class="tn-margin-top" status="nomore"></tn-load-more>
				<view v-else class="empty__item tn-margin-top">
					<tn-empty icon="https://resource.tuniaokj.com/images/empty/alien/2.png" text="空空如也"></tn-empty>
				</view>
			</view>

			<view class="" v-if="current==1">
				<view class="tn-margin" v-for="(item,index) in redpacketList" :key="index">
					<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding website-shadow">
						<view class="justify-content-item">
							<view class="tn-flex tn-flex-col-center tn-flex-row-left">
								<view class="logo-pic tn-shadow">
									<!-- <view class="logo-image">
                    <view class="tn-shadow-blur" style="background-image:url('https://resource.tuniaokj.com/images/blogger/blogger_beibei.jpg');width: 110rpx;height: 110rpx;background-size: cover;">
                    </view>
                  </view> -->
								</view>
								<view class="tn-padding-right tn-color-black">
									<view class="tn-padding-right tn-padding-left-sm tn-text-lg tn-text-bold">
										{{ item.money }}元
									</view>
								</view>
							</view>
						</view>
						<view class="justify-content-item tn-flex-row-center">
							<view v-if="item.draw === 1"
								class="tn-cool-bg-color-6 tn-padding-xs tn-color-white tn-round tn-shadow-blur">
								<!-- <text class="tn-padding-left-xs">开红包</text> -->
								<text class="tn-padding-sm">已领取</text>
								<!-- <text class="tn-icon-send-fill tn-padding-xs"></text> -->
							</view>
							<view v-else
								class="tn-cool-bg-color-15 tn-padding-xs tn-color-white tn-round tn-shadow-blur"
								@click="openRedpacket(item.id)">
								<text class="tn-padding-sm">开红包</text>
								<!-- <text v-else class="tn-padding-left-xs">已领取</text> -->
								<!-- <text class="tn-icon-send-fill tn-padding-xs"></text> -->
							</view>
						</view>
					</view>
				</view>
				<tn-load-more v-if="redpacketList.length" class="tn-margin-top" status="nomore"></tn-load-more>
				<view v-else class="empty__item tn-margin-top">
					<tn-empty icon="https://resource.tuniaokj.com/images/empty/alien/2.png" text="空空如也"></tn-empty>
				</view>
			</view>

		</view>
		<!-- 客服压屏窗-->
		<tn-landscape :show="show" closePosition="bottom" @close="closeLandscape" style="z-index: 99999;">
			<view class="tn-margin-bottom-xl tn-padding-sm" style="width: 500rpx; height: 700rpx; 
      background-color: #d5519b;border-radius:10rpx;">
				<view style="background-color: white; width: 100%;height: 100%;border-radius: 10rpx;">
					<view
						class="tn-flex tn-flex-wrap tn-flex-col-center tn-flex-row-center tn-flex-direction-column tn-padding">
						<view class="tn-flex-col-center tn-flex-row-center">
							<text class="tn-text-xxl tn-flex-col-center tn-flex-row-center"
								style="font-weight: bold;">核销码</text>
						</view>
						<view class="tn-flex-col-center tn-flex-row-center tn-margin-top">
							<text class="tn-text-xxl tn-flex-col-center tn-flex-row-center tn-margin-top tn-color-red"
								style="font-weight: bold;">{{ redeem_code }}</text>
						</view>
					</view>
					<view
						class="tn-flex tn-flex-wrap tn-flex-col-center tn-flex-row-center tn-flex-direction-column tn-padding">
						<!-- <view class="tn-flex-col-center tn-flex-row-center" >
    				<text class="tn-text-xxl tn-flex-col-center tn-flex-row-center" style="font-weight: bold;">核销码</text>
    			</view> -->
						<view class="tn-flex-col-center tn-flex-row-center">
							<text class="tn-text-xxl tn-flex-col-center tn-flex-row-center tn-margin-top tn-color-red"
								style="font-weight: bold;">{{ money }}元</text>
						</view>
					</view>

				</view>
			</view>
			<!-- <image src="https://resource.tuniaokj.com/images/landscape/2022-new-year.png" mode="widthFix"></image> -->
		</tn-landscape>
	</view>
</template>

<script>
	import {
		redpacketList
	} from '../common/vmeitime-http'
	import template_page_mixin from '@/libs/mixin/template_page_mixin.js'
	export default {
		name: 'TemplateProduct',
		mixins: [template_page_mixin],
		data() {
			return {
				share: '',
				show: false,
				redeem_code: "",
				money: "0.00",
				avatar: "",
				nickname: "",
				level: "",
				levelName: "",
				isagent: 0,
				status: 0,
				invitee_num: 0,
				sumMoney: 0.00,
				cardCur: 0,
				current: 0,
				fixedList: [{
						name: '我的粉丝'
					},
					// {
					// 	name: '我的红包'
					// }
				],
				inviteeList: [],
				redpacketList: [],
				content: [],
				countOptions: [],
				customButtonGroups: [],
				tagList: [],
				uid: ''
			}
		},
		onShareAppMessage(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				console.log(res.target)
			}
			return this.share
		},
		onShareTimeline(res) {
			console.log("分享xxxx")
			return this.share
		},
		created() {
			const userInfo = uni.getStorageSync("userInfo")
			this.avatar = userInfo.avatar
			this.nickname = userInfo.nickname
			this.level = userInfo.agentlevel
			this.levelName = userInfo.levelname
			this.isagent = userInfo.isagent
			this.status = userInfo.status
			this.sumMoney = userInfo.credit2
			this.uid = userInfo.id
			this.initInviteList()
			this.getViewer()
		},
		methods: {
			getViewer() {
				this.$api.getviewer({
					uid: this.uid,
					goodsid: uni.getStorageSync('gid')
				}).then(res => {
					this.invitee_num = res.data.n
				})
			},
			withdrawRedpacket() {
				if (this.sumMoney <= 0) {
					uni.showToast({
						title: "余额不足",
						icon: "error",
						mask: true
					})
				} else {
					this.$api.redpacketInfo().then((res) => {
						console.log(res)
						const ret = res.data
						if (!ret.error) {
							uni.showToast({
								title: "提现申请成功",
								icon: 'success',
								mask: true
							})
							this.sumMoney = "0.00"
						} else {
							uni.showToast({
								title: '服务开小差，请重试',
								icon: 'error',
								mask: true
							});
						}
						if (ret.status == 0 && ret.result.message) {
							uni.showToast({
								title: ret.result.message,
								icon: "error",
								mask: true
							})
						}
						// if (res.data.code != 100) {
						// 	uni.showToast({
						// 		title: '服务开小差，请重试',
						// 		icon: 'error',
						// 		mask: true
						// 	});
						// } else {
						// 	this.money = res.data.data.money
						// 	this.redeem_code = res.data.data.settle_code
						// 	this.show = true
						// }
					})
				}
			},
			openRedpacket(_id) {
				this.$api.drawRedpacket({
					id: _id
				}).then((res) => {
					console.log(res)
					// this.redeem_code = res.data.data.redeem_code
					if (res.data.code != 100) {
						uni.showToast({
							title: '红包领取失败',
							icon: 'error',
							mask: true
						});
					} else {
						this.redpacketList.forEach(item => {
							if (item.id === _id) {
								item.draw = 1;
							}
						});
						console.log(this.redpacketList)
						uni.showToast({
							title: '红包领取成功',
							icon: 'success',
							mask: true
						});

					}
				})
				// this.show = true  
			},
			// 关闭红包
			closeLandscape() {
				this.$api.redpacketBalance({}).then((res) => {
					console.log(res)
					// this.redeem_code = res.data.data.redeem_code
					if (res.data.code === 100) {
						this.sumMoney = res.data.data
					}
				})
				this.redeem_code = ""
				this.show = false
			},
			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				});
			},
			initInviteList() {
				this.$api.inviteList({
					gid: uni.getStorageSync('gid'),
					page: 1,
					perPage: 10
				}).then((res) => {
					this.inviteeList = res.data.fans
				})
			},
			// tab选项卡切换
			tabChange(index) {
				this.current = index
				if (this.current === 0) {
					this.initInviteList()
				} else {
					this.$api.redpacketList({
						page: 1,
						perPage: 10
					}).then((res) => {
						this.redpacketList = res.data.data
					})
				}
			},
			getRandomCoolBg() {
				return this.$t.colorUtils.getRandomCoolBgClass()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.template-product {}

	.tn-tabbar-height {
		min-height: 120rpx;
		height: calc(140rpx + env(safe-area-inset-bottom) / 2);
	}

	/* 用户头像 start */
	.logo-image {
		width: 100rpx;
		height: 100rpx;
		position: relative;
	}

	.logo-pic {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
		border-radius: 10rpx;
		overflow: hidden;
		// background-color: #FFFFFF;
	}

	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 100%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.15);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

		&:before {
			content: " ";
			width: 1rpx;
			height: 110%;
			position: absolute;
			top: 22.5%;
			left: 0;
			right: 0;
			margin: auto;
			transform: scale(0.5);
			transform-origin: 0 0;
			pointer-events: none;
			box-sizing: border-box;
			opacity: 0.7;
			background-color: #FFFFFF;
		}
	}

	/* 轮播视觉差 start */
	.card-swiper {
		height: 160rpx !important;
	}

	.card-swiper swiper-item {
		width: 750rpx !important;
		left: 0rpx;
		box-sizing: border-box;
		padding: 0rpx 30rpx 0rpx 30rpx;
		overflow: initial;
	}

	.card-swiper swiper-item .swiper-item {
		width: 100%;
		display: block;
		height: 100%;
		transform: scale(1);
		transition: all 0.2s ease-in 0s;
		border-radius: 10rpx;
		overflow: hidden;
	}

	.card-swiper swiper-item.cur .swiper-item {
		transform: none;
		transition: all 0.2s ease-in 0s;
	}

	.image-banner {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.image-banner image {
		width: 100%;
		height: 100%;
	}

	/* 轮播指示点 start*/
	.indication {
		z-index: 9999;
		width: 100%;
		height: 36rpx;
		position: absolute;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.spot {
		background-color: #FFFFFF;
		opacity: 0.6;
		width: 10rpx;
		height: 10rpx;
		border-radius: 20rpx;
		top: -40rpx;
		margin: 0 8rpx !important;
		position: relative;
	}

	.spot.active {
		opacity: 0.8;
		width: 20rpx;
		background-color: #FFFFFF;
	}

	/* 间隔线 start*/
	.tn-strip-bottom {
		width: 100%;
		border-bottom: 20rpx solid rgba(241, 241, 241, 0.8);
	}

	/* 间隔线 end*/
	/* 标题 start */
	.nav_title {
		-webkit-background-clip: text;
		color: transparent;

		&--wrap {
			position: relative;
			display: flex;
			height: 120rpx;
			font-size: 46rpx;
			align-items: center;
			justify-content: center;
			font-weight: bold;
			background-image: url(https://resource.tuniaokj.com/images/title_bg/title44.png);
			background-size: cover;
		}
	}

	/* 标题 end */


	/* 标签内容 start*/
	.tn-tag-content {
		&__item {
			display: inline-block;
			line-height: 45rpx;
			padding: 10rpx 30rpx;
			margin: 20rpx 20rpx 5rpx 0rpx;

			&--prefix {
				padding-right: 10rpx;
			}
		}
	}

	/* 标签内容 end*/

	/* 内容图 start */
	.content-backgroup {
		z-index: -1;

		.backgroud-image {
			border-radius: 15rpx;
			width: 100%;
		}
	}

	/* 内容图 end */

	/* 商家商品 start*/
	.tn-blogger-content {
		&__wrap {
			box-shadow: 0rpx 0rpx 50rpx 0rpx rgba(0, 0, 0, 0.07);
			border-radius: 20rpx;
			margin: 15rpx;
		}

		&__info {
			&__btn {
				margin-right: -12rpx;
				opacity: 0.5;
			}
		}

		&__label {
			&__item {
				line-height: 45rpx;
				padding: 0 10rpx;
				margin: 5rpx 18rpx 0 0;

				&--prefix {
					color: #E83A30;
					padding-right: 10rpx;
				}
			}

			&__desc {
				line-height: 35rpx;
			}
		}

		&__main-image {
			border-radius: 16rpx 16rpx 0 0;

			&--1 {
				max-width: 690rpx;
				min-width: 690rpx;
				max-height: 400rpx;
				min-height: 400rpx;
			}

			&--2 {
				max-width: 260rpx;
				max-height: 260rpx;
			}

			&--3 {
				height: 212rpx;
				width: 100%;
			}
		}

		&__count-icon {
			font-size: 24rpx;
			padding-right: 5rpx;
		}
	}

	.image-book {
		padding: 150rpx 0rpx;
		font-size: 16rpx;
		font-weight: 300;
		position: relative;
	}

	.image-picbook {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border-radius: 15rpx 15rpx 0 0;
	}

	.website-shadow {
		border-radius: 15rpx;
		box-shadow: 0rpx 0rpx 50rpx 0rpx rgba(0, 0, 0, 0.07);
	}

	.logo-pic {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border: 8rpx solid rgba(255, 255, 255, 0.05);
		box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
		border-radius: 50%;
		overflow: hidden;
		// background-color: #FFFFFF;
	}
</style>